//
//  MineView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 23.03.25.
//

import SwiftUI

struct MineView: View {
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var appState: AppState
    
    @State var showSheet = false
    @State private var selectedDate: Date = DateUtils.date(year: 2024, month: 5)
    @State var isShow = false
    @State var userHeading:URL = KoiAccountingUser.defaultAvatar
    @State var nickname:String = ""
    
    @StateObject var viewModel = MineViewModel()
    
    var body: some View {
        VStack {
            ScrollView {
                HStack {
                    AsyncImage(url:userHeading) { phase in
                        switch phase {
                        case .empty:
                            Image("koi_default_avatar128")
                                .resizable()
                                .scaledToFit()
                                .frame(width: 60, height: 60)
                        case .success(let image):
                            image
                                .resizable()
                                .scaledToFit()
                                .frame(width: 60, height: 60)
                                .cornerRadius(30)
                        case .failure:
                            Image("koi_default_avatar128")
                                .resizable()
                                .scaledToFit()
                                .frame(width: 60, height: 60)
                        @unknown default:
                            Image("koi_default_avatar128")
                                .resizable()
                                .scaledToFit()
                                .frame(width: 60, height: 60)
                        }
                    }
                    VStack (alignment: .leading){
                        Text(nickname)
                            .font(.system(size: 20))
                            .fontWeight(.medium)
                            .foregroundColor(Color.koiColorTitle)
                        NavigationLink(destination: { UserProfileView() }, label: {
                            HStack {
                                Text("编辑个人资料")
                                    .font(.system(size: 13))
                                    .foregroundStyle(Color.koiColorTitleSecond)
                                Image("koi_ic_arrow_right")
                                    .resizable()
                                    .frame(width: 16, height: 16)
                            }
                        })
                    }
                    Spacer()
                }
                .padding(.bottom, 14)
                HStack {
                    VStack {
                        Text(viewModel.achivevmentDay)
                            .font(.system(size: 26))
                            .fontWeight(.medium)
                            .foregroundColor(Color.koiColorTitle)
                        Text("记账天数")
                            .font(.system(size: 13))
                            .foregroundColor(Color.koiColorTitleSecond)
                    }
                    .padding(.vertical, 16)
                    .frame(maxWidth: .infinity)
                    .background(content: {
                        Image("koi_mine_achievement_bg")
                    })
                    .background(Gradient(colors: [Color(hex: "D9F2FE"), Color(hex: "ECF9FF").opacity(0.6)]))
                    .cornerRadius(16)
                    .padding(.trailing, 8)
                    VStack {
                        Text(viewModel.achivevmentTotalNumber)
                            .font(.system(size: 26))
                            .fontWeight(.medium)
                            .foregroundColor(Color.koiColorTitle)
                        Text("记账笔数")
                            .font(.system(size: 13))
                            .foregroundColor(Color.koiColorTitleSecond)
                    }
                    .padding(.vertical, 16)
                    .frame(maxWidth: .infinity)
                    .background(Gradient(colors: [Color(hex: "FFEEF3"), Color(hex: "FFEEF3").opacity(0.48), Color(hex:"FFEEF3").opacity(0.35)]))
                    .cornerRadius(16)
                    .padding(.leading, 8)
                }
                
                VStack {
                    NavigationLink(
                        destination: { BookManagerView() },
                        label: { KoiSettingItemView(itemImageRes: "koi_ic_item_book", itemTitle: "账本管理") }
                    )
                    
                    NavigationLink(
                        destination: {CategoryManagerView()},
                        label: {KoiSettingItemView(itemImageRes: "koi_ic_item_category", itemTitle: "类别管理")}
                    )
                    
                    // 标签功能暂不开放
//                    NavigationLink(
//                        destination: { TagManagerView() },
//                        label: { KoiSettingItemView(itemImageRes: "koi_ic_item_label", itemTitle: "标签管理") }
//                    )
                    
                    NavigationLink(
                        destination: { BudgetManagerView() },
                        label: { KoiSettingItemView(itemImageRes: "koi_ic_item_budget", itemTitle: "预算管理") }
                    )
                }
                .background(Color.koiColorBackgroundItem)
                .cornerRadius(16)
                .padding(.top, 16)
                
                VStack {
                    NavigationLink(
                        destination: { FeedbackView() },
                        label: { KoiSettingItemView(itemImageRes: "koi_ic_item_feedback", itemTitle: "意见反馈") }
                    )
                    
//                    Button(action: {
//                        // 分享 app 到微信
//                    }) {
//                        KoiSettingItemView(itemImageRes: "koi_ic_item_share", itemTitle: "分享给朋友")
//                    }
                    
                    Button(action: {
                        // app store 是否可以求赞
                        // 跳转到 App Store
                        if let url = URL(string: "itms-apps://itunes.apple.com/app/6744814322") {
                            UIApplication.shared.open(url, options: [:], completionHandler: nil)
                        }
                    }) {
                        KoiSettingItemView(itemImageRes: "koi_ic_item_like", itemTitle: "夸夸锦鲤～")
                    }
                    
                    NavigationLink(
                        destination: { AboutView() },
                        label: { KoiSettingItemView(itemImageRes: "koi_ic_item_about", itemTitle: "关于") }
                    )
                }
                .background(Color.koiColorBackgroundItem)
                .cornerRadius(16)
                .padding(.vertical, 16)
                
                // debug 模块
                #if DEBUG || DEBUGLOCAL
                VStack {
                    NavigationLink(
                        destination: { DebugView() },
                        label: { KoiSettingItemView(systemName: "wrench.and.screwdriver.fill", itemTitle: "调试中心") }
                    )
                }
                .background(Color.koiColorBackgroundItem)
                .cornerRadius(16)
                .padding(.vertical, 16)
                #endif
            }
        }
        .padding(.horizontal, 20)
        .background(Color.koiColorBackground)
        .koiToolbar(title: Text("我的"))
        .onAppear {
            if let user = KoiAccountingUserManager.standard.user() {
                userHeading = user.getHeadImg()
                nickname = user.getNickname()
            }
            viewModel.loadData()
        }
    }
}

struct MineItemView : View {
    
    var itemImage: Image
    var itemTitle: String
    
    init(itemImageRes: String, itemTitle: String) {
        self.itemImage = Image(itemImageRes)
        self.itemTitle = itemTitle
    }
    
    init(systemName: String, itemTitle: String) {
        self.itemImage = Image(systemName: systemName)
        self.itemTitle = itemTitle
    }
    
    var body: some View {
        HStack {
            itemImage
                .resizable()
                .frame(width: 20, height: 20)
                .foregroundColor(Color.koiColorTitle)
            Text(itemTitle)
                .font(.system(size: 14))
                .fontWeight(.medium)
                .foregroundColor(Color.koiColorTitle)
            Spacer()
            Image("koi_ic_arrow_right")
                .resizable()
                .frame(width: 16, height: 16)
        }.padding(EdgeInsets(top: 18, leading: 14, bottom: 18, trailing: 14))
    }
}
    
#Preview {
    MineView()
}
